/* rank data */

use entropy_bible, clear
merge m:1 trans using data_bible_final, keep(3)

keep if book==40|book==41|book==42|book==43|book==44|book==66


bysort trans: gen count=_N
drop if count<6

/* averaged over languages per book */
collapse (mean) D*, by(language ISO book) fast

bysort book: egen rank_structure=rank(D_structure), field
bysort book: egen rank_order=rank(D_order), field

/* one column per book */
foreach book in 40 41 42 43 44 66 {
	preserve
	keep if book==`book'
	keep rank* language ISO
	ren rank_structure rank_structure_`book'
	ren rank_order rank_order_`book'
	save book`book', replace
	restore
}

/* merge books */
use book40, clear

foreach book in 41 42 43 44 66 {
	merge 1:1 language ISO using book`book', nogenerate
}

/*generate sperman correlation matrix*/
order rank_str* rank_order*

qui pwcorr r*

mat mat=r(C)

plotmatrix, s(-1(.25)1) m(mat) allcolors(red*1 red*0.8 red*0.6 red*0.4 green*0.4 green*0.6 green*0.8 green*1.0 ) freq ///
formatcells(%3.1f)  legend(off) maxticks(12) nodiag ///
ytitle("Word order             Word structure") xtitle("Word structure             Word order") ///
xlabel(1 `"Matthew"' 2 `"Mark"' 3 `"Luke"' 4 `"John"' 5 `"Acts"' 6 `"Revelation"' 7 `"Matthew"' 8 `"Mark"' 9 `"Luke"' 10 `"John"' 11 `"Acts"' 12 `"Revelation"', angle(90)) ///
ylabel(0 `"Matthew"' -1 `"Mark"' -2 `"Luke"' -3 `"John"' -4 `"Acts"' -5 `"Revelation"' -6 `"Matthew"' -7 `"Mark"' -8 `"Luke"' -9 `"John"' -10 `"Acts"' -11 `"Revelation"',angle(0)) ///
ysize(1) xsize(1)

graph export fig2.png, height(10000) replace  
window manage close graph 

foreach book in 40 41 42 43 44 66 {
	capture erase book`book'.dta
}

exit
